const wxCharts = require("../../utils/wxcharts-min.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.globalData.imgUrl,          // 线上图片
    localImg: app.globalData.staticimgUrl,  // 本地图片
    current: 1,             // 选项卡切换
    domMove: 'move1',       // 左右平移动画
    latitude: "",
    longitude: "",
    cityMarkers:[],         // 医院信息
    machineMarkers:[],      // 设备信息
    city:false,
    machine:false,
    location:'',            // 当前位置经纬度坐标
    cityData:{}             // 省市县信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getPosition();
    this.mapCtx = wx.createMapContext('myMap');
    
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    
  },

  //点击地图列表上移
  moveUp(e){
    const whichOne = e.currentTarget.dataset.one;
    if ( whichOne == 'city'){
      this.setData({
        city: !this.data.city
      })
    }
    if (whichOne == 'machine') {
      this.setData({
        machine: !this.data.machine
      })
    }
  },
  //进入详情
  entryDetail(e){
    const siteId = e.currentTarget.dataset.siteid;
    const type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/hospitalDetail/hospitalDetail?siteId=' + siteId + "&type=" + type
    });
  },
  //获取用户当前位置经纬度
  getPosition() {
    const that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
       
        that.getPos();
      }
    })
  },

  //获取城市所有医院信息
  getPos() {
    const that = this;
    const param = {
      userId: wx.getStorageSync('userid'),
      longitude: this.data.longitude,
      dimensionality: this.data.latitude
    };
    const localImg = app.globalData.staticimgUrl;  // 本地图片
    app.request({
      url: 'citycore/citycoreapi/getList',
      data: param,
      success: function(res) {
        const data = res.data;
        let marker = [];
        let obj = {};
        for (let i = 0; i < data.length; i++) {
          obj = {
            iconPath: localImg+"hospital_icon.png",
            id: data[i].id,
            latitude: data[i].dimensionality,
            longitude: data[i].longitude,
            callout: {
              content: data[i].name ,
              color: '#ffffff',
              fontSize: 14,
              borderWidth: 6,
              borderColor: '#9d46fa',
              display: 'BYCLICK',
              textAlign: 'center',
              borderRadius: 4,
              bgColor: '#9d46fa'
            },
            name:data[i].name,
            title: data[i].name,
            distance: data[i].distance,
            address: data[i].addressMap,
            phone: data[i].phone,
            siteId: data[i].siteId,
            width: 30,
            height: 38
          }
          marker.push(obj)
        }
       
        that.setData({
          cityMarkers: marker
        })
      }
    });
  },
  // 图表
  chartHot() {
    let res = wx.getSystemInfoSync();
    let screenWith = res.screenWidth;
    app.request({
      url: 'doctor/beautyHot/getList',
      method:"GET",
      success: function (res) {
        new wxCharts({
          canvasId: 'radarCanvas',
          type: 'radar',
          categories: res.data.service,
          series: [{
            name: '上月',
            data: res.data.lastMonth
          },
          {
            name: '本月',
            data: res.data.nowMonth
          }],
          width: screenWith,
          height: 450,
          extra: {
            radar: {
              max: 300
            }
          }
        });
      },
      fail: function (res) {}
    });
  },
  //设备分布
  machine(){
    const that = this;
    this.mapCtx = wx.createMapContext('myMap');
    const param = {
      userId: wx.getStorageSync('userid'),
      longitude: this.data.longitude,
      dimensionality: this.data.latitude
    };
    const localImg = app.globalData.staticimgUrl;  // 本地图片
    app.request({
      url: 'citycore/deviceapi/getList',
      data: param,
      success: function (res) {
        const data = res.data;
        let marker = [];
        let obj = {};
        for (let i = 0; i < data.length; i++) {
          obj = {
            iconPath: localImg+"device_icon.png",
            id: data[i].id,
            latitude: data[i].dimensionality,
            longitude: data[i].longitude,
            callout: {
              content: data[i].name,
              color: '#ffffff',
              fontSize: 14,
              borderWidth: 6,
              borderColor: '#9d46fa',
              display: 'BYCLICK',
              textAlign: 'center',
              borderRadius: 4,
              bgColor: '#9d46fa'
            },
            name: data[i].name,
            distance: data[i].distance,
            address: data[i].siteAddress,
            phone: data[i].sitePhone,
            siteId: data[i].siteId,
            title: data[i].name,
            width: 30,
            height: 38
          }
          marker.push(obj)
        }

        that.setData({
          machineMarkers: marker
        })
      }
    });
  },

  //切换导航
  tabCurrent(e) {
    const current = e.currentTarget.dataset.index;
    current == 2 && this.chartHot();
    current == 3 && this.machine();
    const domMove = current == '1' ? 'move1' : (current == '2' ? 'move2' : 'move3');
    this.setData({
      current: current,
      domMove
    });
  }
})